
import { useRef, useEffect } from "react";
export default (cb) => {
    if (typeof performance === 'undefined' || typeof window === 'undefined') return
    const cbRef = useRef(cb)
    const frame = useRef()
    const init = useRef(performance.now())
    const last = useRef(performance.now())

    const animate = now => {
        cbRef.current({ time: (now - init.current) / 1000, delta: (now - last.current) / 1000 })
        last.current = now
        frame.current = requestAnimationFrame(animate)
    }
    useEffect(() => {
        frame.current = requestAnimationFrame(animate)
        return () => {
            frame.current && cancelAnimationFrame(frame.current)
        }
    }, [])
}